<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">数据库管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>数据库管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <input id="db-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="db-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="db-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>新增数据库</button>
            <button id="db-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
        </div>

        <table class="layui-table" id="db-table" lay-filter="db-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="db-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="table"><span class="layui-icon layui-icon-table" title="数据表"></span></a>
    <a class="layui-btn layui-btn-default layui-btn-xs" lay-event="edit"><span class="layui-icon layui-icon-edit" title="修改"></span></a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><span class="layui-icon layui-icon-delete" title="删除"></span></a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="db-tpl-state">
    <input type="checkbox" lay-filter="user-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.enabled==true?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        var renderTable = function () {
            table.render({
                elem: '#db-table',
                // url: config.base_server + 'user_query.json',
                url: config.base_server + 'api-db/db/getDBList',
                method: 'GET',
                where: {
                    access_token: config.getToken().access_token
                },
                page: true,
                cols: [[
                    {type: 'numbers'},
                    {field: 'dbName', title: '数据库名'},
                    {field: 'dbUrl', title: '数据库地址'},
                    {field: 'note', title: '备注'},
                    {align: 'center', toolbar: '#db-table-bar', title: '操作', width: 150}
                ]]
            });
        };
        renderTable();
        // 添加按钮点击事件
        $('#db-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(db-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除该数据库吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-db/db/deleteDB/' + obj.data.dbId, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            renderTable();
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');
                });
            }
            else if (layEvent === 'table'){
                admin.putTempData("t_db",data);
                Q.go("dbtables");
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            admin.putTempData('t_db', data);
            var title = data ? '修改数据库' : '新增数据库';
            admin.popupCenter({
                title: title,
                path: 'pages/business/db_form.html',
                finish: function () {
                    table.reload('db-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#db-btn-search').click(function () {
            var value = $('#db-search-value').val();
            table.reload('db-table', {where: {param: value}});
        });


        // 导出
        $('#db-btn-export').click(function () {
            var url = config.base_server + 'api-db/db/exportDB?access_token=' + config.getToken().access_token;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = "blob";
            xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            xhr.onload = function () {
                if (this.status == 200) {
                    var blob = this.response;
                    var objecturl = URL.createObjectURL(blob);
                    window.location.href = objecturl;
                }
            }
            xhr.send();
        });

        // 修改数据库状态
        form.on('switch(db-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-user/users/updateEnabled', {
                id: obj.elem.value,
                enabled: obj.elem.checked ? true : false
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
    });
</script>